<?php
class cmshopapi_GetOrderTransaction extends cmshopApi_base
{
    var $verb='GetOrderTransaction';
    var $fields=array(
        
    );
    function Request(){
        if(strlen($this->input['order_id'])){
            return $this->Response($order_id);
        }
        return false;
    }
    
    // 回给 客户  .
    function Response($order_id,$attach=null){
        //Orders
        if($order_id instanceof Orders){
            $order=$order_id;
        }else{
            $order=Orders::find('order_id=? And store_id=?',$order_id,$this->token['store_id'])->getOne();
        }
        $xmlArr_o=array(
            'order_id'=>$order->order_id,
            'store_name'=>$order->store->name,
            'total_amount'=>$order->total_amount,
            'finel_amount'=>$order->finel_amount,
            'shipping_cost'=>$order->shipping_cost,
            'currency'=>$order->currency,
            'paid_time'=>($order->paidtime?$this->dateTime($order->paidtime):0),
            'status_payment'=>$order->status_payment,
            'status_shiped'=>$order->status_shiped,
            'payment'=>$order->payment,
            'shipment'=>$order->shipment_id,
            'created'=>$this->dateTime($order->created),
        );
        //Transaction
        $xmlArr_t=array();
        $ts=Transaction::find('order_id=? And store_id=? ',$order->order_id,$order->store_id)->getAll();
        if(count($ts)) foreach($ts as $t){
            $xmlArr_t[]=array(
                'transaction_id'=>$t->transaction_id,
                'created'=>$this->dateTime($t->created),
                'quantity_purchased'=>$t->quantity,
                'platform'=>'',
                'title'=>'<![CDATA['.$t->title.']]>',
                'status_payment'=>$t->status_payment,
                'transaction_price'=>$t->price,
                'shipping_cost'=>$t->shipping_cost,
//                 'transaction_price'=>$t->price,
                'currency'=>$t->currency,
                'property_id'=>$t->property_id,
                'item'=>array(
                    'item_id'=>$t->goods->goods_id,
                    'sn'=>$t->goods->sn,
                    'status'=>$t->goods->listingstatus,
                    'stock'=>$t->goods->stock,
                    'custom'=>$t->goods->custom,
                    'seller_id'=>$t->goods->seller_id,
                    'store_id'=>$t->goods->store_id,
                )
            );
        }
        // buyer
        $xmlArr_b=array(
            'shipping_address'=>array(
                'email'=>$order->ship_email,
                'gender'=>$order->ship_gender,
                'first_name'=>$order->ship_firstname,
                'last_name'=>$order->ship_lastname,
                'company'=>$order->ship_company,
                'cityname'=>$order->ship_cityname,
                'stateorprovince'=>$order->ship_stateorprovince,
                'country'=>$order->ship_country,
                'countryname'=>$order->ship_countryname,
                'street1'=>$order->ship_street1,
                'street2'=>$order->ship_street2,
                'postalcode'=>$order->ship_postalcode,
                'phone'=>$order->ship_phone,
            )
        );
        $xmlArr=array(
            'Ack'=>'Success',
            'timestamp'=>$this->dateTime(),
		) + $xmlArr_o;
		$xmlArr['transactions']=$xmlArr_t;
		//$xmlArr['transactions']=$xmlArr_t;
		$xmlArr['buyer']=$xmlArr_b;
		if(is_array($attach)){
            $xmlArr=$xmlArr+$attach;
        }
        return $this->xmlresponse($xmlArr);
    }
    
}